﻿using AkazoNobel.Scheduler.Core;
using AkazoNobel.Scheduler.StoreMasterData.Common;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Model;
using AkazoNobel.Scheduler.StoreMasterData.DataAccess.Service;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace AkazoNobel.Scheduler.StoreMasterData.Tasks
{
    public class TintingDataSync : BaseTask
    {
        public List<TintingSyncModel> GetTintingSyncList()
        {
            List<TintingSyncModel> syncList = new List<TintingSyncModel>();
            syncList = TintingDataSyncService.GetSyncList();

            foreach (var item in syncList)
            {
                #region dispenser_type_id
                if (item.dispenser_type_id == "BT-16")
                {
                    item.dispenser_type_id = "1810001";
                }
                else if (item.dispenser_type_id == "D200")
                {
                    item.dispenser_type_id = "1810002";
                }
                else if (item.dispenser_type_id == "TM300 1.1")
                {
                    item.dispenser_type_id = "1810003";
                }
                else if (item.dispenser_type_id == "53PD")
                {
                    item.dispenser_type_id = "1810004";
                }
                else if (item.dispenser_type_id == "53XD")
                {
                    item.dispenser_type_id = "1810005";
                }
                else if (item.dispenser_type_id == "M3")
                {
                    item.dispenser_type_id = "1810006";
                }
                else if (item.dispenser_type_id == "TM300 1.0")
                {
                    item.dispenser_type_id = "1810007";
                }
                else if (item.dispenser_type_id == "TM280")
                {
                    item.dispenser_type_id = "1810008";
                }
                else if (item.dispenser_type_id == "不需要")
                {
                    item.dispenser_type_id = "1810009";
                }
                #endregion
                #region pc_type_id
                if (item.pc_type_id.Contains("新购"))
                {
                    item.pc_type_id = "1830001";
                }
                else if (item.pc_type_id == "无")
                {
                    item.pc_type_id = "1830003";
                }
                else
                {
                    item.pc_type_id = "1830002";
                }
                #endregion
                #region shaker_type_id
                if (item.shaker_type_id == "SHH-IV")
                {
                    item.shaker_type_id = "1820001";
                }
                else if (item.shaker_type_id == "SO-40A")
                {
                    item.shaker_type_id = "1820002";
                }
                else if (item.shaker_type_id == "Vibro")
                {
                    item.shaker_type_id = "1820003";
                }
                else if (item.shaker_type_id == "三华S5")
                {
                    item.shaker_type_id = "1820004";
                }
                else if (item.shaker_type_id == "不需要")
                {
                    item.shaker_type_id = "1820005";
                }
                #endregion
                #region delivery_type
                if (item.businesskey.Contains("DD"))
                {
                    item.DeliveryType = "1800007";
                }
                else if (item.DeliveryType == "新机安装(STD)")
                {
                    item.DeliveryType = "1800001";
                }
                else if (item.DeliveryType == "旧机转让(STD)" || item.DeliveryType.Contains("STD"))
                {
                    item.DeliveryType = "1800002";
                }
                else if (item.DeliveryType == "MT Automatic Machine Move(STD)")
                {
                    item.DeliveryType = "1800003";
                }
                else if (item.DeliveryType == "MT Manual Manchine Move(STD)")
                {
                    item.DeliveryType = "1800004";
                }
                else if (item.DeliveryType == "新机安装(DIR)")
                {
                    item.DeliveryType = "1800005";
                }
                else if (item.DeliveryType == "旧机转让(DIR)" || item.DeliveryType.Contains("DIR"))
                {

                    item.DeliveryType = "1800006";
                }

                #endregion
                #region tinting_status_id
                if (item.tinting_status_id.Contains("新建"))
                {
                    item.tinting_status_id = "1790001";
                }
                else if (item.tinting_status_id.Contains("使用"))
                {
                    item.tinting_status_id = "1790002";
                }
                else if (item.tinting_status_id.Contains("经销商寄存"))
                {
                    item.tinting_status_id = "1790003";
                }
                else if (item.tinting_status_id.Contains("公司大仓"))
                {
                    item.tinting_status_id = "1790004";
                }
                else if (item.tinting_status_id.Contains("报废"))
                {
                    item.tinting_status_id = "1790005";
                }
                else if (item.tinting_status_id.Contains("旧机更换"))
                {
                    item.tinting_status_id = "1790006";
                }
                #endregion
            }
            return syncList;
        }

        /// <summary>
        /// post json字符串到CRM 接口
        /// </summary>
        /// <param name="syncData">需要同步的数据列表</param>
        /// <returns></returns>
        public HttpResult PostDataToCrm(List<TintingSyncModel> syncData)
        {
            HttpResult result = new HttpResult();
            string postJson = TintingDataSyncService.List2Json(syncData);
            Logger.Error(postJson);

            if (postJson != string.Empty)
            {
                result = TintingDataSyncService.RestPost(postJson);
            }

            return result;
        }

        public override void ExecuteTask(Quartz.IJobExecutionContext context)
        {
            //execute procedure
            //string connectionName = Constant.SMD_DB_NAME;
            //SqlConnectionHelper.RunProcedure("SMDtoCRMsync", new SqlParameter[] { }, 600, connectionName);

            List<TintingSyncModel> syncModelList = new List<TintingSyncModel>();
            syncModelList = GetTintingSyncList();



            HttpResult result = PostDataToCrm(syncModelList);

            if (result.StatusCode == System.Net.HttpStatusCode.OK)
            {
                TintingDataSyncService.UpdateSyncStatus(syncModelList, SyncStatus.Success, result.StatusDescription);
            }
            else
            {
                TintingDataSyncService.UpdateSyncStatus(syncModelList, SyncStatus.Failed, result.StatusDescription);
            }
        }
    }


}
